<?php 
/**
* Copyright (C)2001-2099 Zeai.cn All rights reserved.
* E-Mail：supdes#qq.com　QQ:7144100,797311
* http://www.zeai.cn
* http://www.a9a.cn
* http://www.yzlove.com
* Last update 2021/09/30 by supdes
*/
require_once '../sub/init.php';
require_once ZEAI.'sub/conn.php';
require_once ZEAI.'cache/config_vip.php';
require_once ZEAI.'cache/config_pay.php';
require_once ZEAI.'sub/www_zeai_cn_sms.php';
$currfields = 'sex,grade,loveb,openid,subscribe,money,sjtime,sjtime2,nickname,magapp_uid,crm_qxnum,admid,vipnum';
//if($iflogin==0 && $kind==4 && !ifint($cook_uid)){
$cook_uid=intval($cook_uid);
if(($iflogin==0 || $iflogin=='undefined') && ($kind==4 || $kind==11 || $kind==12 || $kind==7 || $kind==24 ) && !ifint($cook_uid)){
	$data_openid = $cook_openid;
	//游客报名
}else{
	if($kind==20 && ifint($cook_uid)){
		$row = $db->ROW(__TBL_USER__,$currfields,"id=".$cook_uid);
	}else{
		require_once ZEAI.'m/my_chk_u.php';
	}
	$data_sex    = $row['sex'];
	$data_money  = $row['money'];
	$data_grade  = $row['grade'];
	$data_sjtime = $row['sjtime'];
	$data_sjtime2= $row['sjtime2'];
	$data_openid = $row['openid'];
	$data_subscribe = $row['subscribe'];
	$data_nickname  = trimhtml(dataIO($row['nickname'],'out'));
	$magapp_uid = $row['magapp_uid'];
	$data_vipnum = $row['vipnum'];
	//单次牵线
	$data_crm_qxnum = $row['crm_qxnum'];
	$data_admid     = $row['admid'];
}
$loveb_buy = json_decode($_VIP['loveb_buy'],true);
switch ($submitok) {
	case 'ajax_pay_money_loveb':
		//kind，1:会员升级,2:loveb充值,3:余额充值,3:余额充值，4活动报名费，5:全民红娘升级tg，6:推广注册激活，7：认证，8：文章打赏，9：用户注销，11：商家入駐，12：商品购买
		$jump_url = urldecode($jump_url);
		switch ($kind) {
			case 1://会员升级
				$sj_rmb1 = json_decode($_VIP['sj_rmb1'],true);
				$sj_rmb2 = json_decode($_VIP['sj_rmb2'],true);
				$price   = ($data_sex==2)?$sj_rmb2[$grade.'_'.$if2]:$sj_rmb1[$grade.'_'.$if2];
				if($price!=$money)json_exit(array('flag'=>0,'msg'=>'价格异常，升级失败'));
				$paymoney = abs(round($money,2));
				//
				$orderid       = 'VIP-'.$cook_uid.'-'.date("YmdHis");
				$orderid_title = utitle($grade).'VIP升级('.get_if2_title($if2).')';
			break;
			case 2://loveb充值
				$paymoney = abs(round($money,2));
				$orderid_title = utitle($data_grade).$_ZEAI['loveB'].'充值';
				$orderid    = 'LOVEB-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 3://余额充值
				$paymoney = abs(round($money,2));
				$orderid_title = utitle($data_grade).'余额充值';
				$orderid = 'MONEY-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 4://活动报名
				$paymoney = abs(round($money,2));
				$orderid_title = utitle($data_grade).'活动报名';
				//$return_url = HOST.'/?z=party&e=detail&a=11';见jumpurl
				//$jump_url   = "detail";//m1/party_detail.php
				$orderid = 'PARTY-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 7://认证
				$paymoney = abs(round($money,2));
				$orderid_title = '诚信认证';
				$orderid = 'RZ-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 8://文章赞赏
				$paymoney = abs(round($money,2));
				$orderid_title = '文章赞赏';
				$orderid = 'WZ-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 9://注销资料
				$paymoney = abs(round($money,2));
				$orderid_title = $content;
				$orderid = 'DEL-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 13://互选相亲
				$paymoney = abs(round($money,2));
				$orderid_title = '互选相亲报名';
				$orderid = 'HX-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 19://置顶
				$paymoney = abs(round($money,2));
				$orderid_title = '置顶';
				$orderid = 'TOP-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 20://付费注册
				$paymoney = abs(round($money,2));
				$orderid_title = '新用户注册审核费';
				$orderid = 'REG-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 21://单身群
				$paymoney = abs(round($money,2));
				$orderid_title = '单身群入群费';
				$orderid = 'QUN-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 22://情感测试
				$paymoney = abs(round($money,2));
				$orderid_title = '情感测试费';
				$orderid = 'CS-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 23://单次牵线
				$paymoney = abs(round($money,2));
				$orderid_title = '单次牵线';
				$orderid = 'QX-'.$cook_uid.'-'.date("YmdHis");
			break;
			case 24://盲盒
				$paymoney = abs(round($money,2));
				$orderid_title = '脱单盲盒';
				$orderid = 'MH-'.$cook_uid.'-'.date("YmdHis");
			break;
		}
		if ($paykind=='wxpay' || $paykind=='alipay'){
			$money_list_id = intval($grade);
			$paytime       = intval($if2);
			if($kind == 4){//活动报名费
				$tmparrp=explode('fid=',urldecode($jump_url));
				$fid=intval($tmparrp[1]);
				if(ifint($fid))$money_list_id=$fid;
			}elseif($kind == 8){//文章赞赏
				$tmparrp=explode('id=',urldecode($jump_url));
				$id=intval($tmparrp[1]);
				if(ifint($id))$money_list_id=$id;
			}elseif($kind == 13){//互选相亲报名费
				$tmparrp=explode('id=',urldecode($jump_url));
				$id=intval($tmparrp[1]);
				if(ifint($id))$money_list_id=$id;
			}elseif($kind == 19){//置顶
				$money_list_id = intval($objid);
			}elseif($kind == 21){//单身群入群费
				$tmparrp=explode('id=',urldecode($jump_url));
				$id=intval($tmparrp[1]);
				if(ifint($id))$money_list_id=$id;
			}elseif($kind == 22){//情感测试费
				$tmparrp=explode('id=',urldecode($jump_url));
				$id=intval($tmparrp[1]);
				if(ifint($id))$money_list_id=$id;
			}elseif($kind == 23){//单次牵线
				$money_list_id = intval($qx_admid);
				$paytime       = intval($qx_uid);
			}elseif($kind == 24){//脱单盲盒
				$tmparrp=explode('id=',urldecode($jump_url));
				$fid=intval($tmparrp[1]);
				$json = encode_json(array('uid'=>$cook_uid,'openid'=>$cook_openid,'fid'=>$fid,'inout'=>$inout));
			}
			$db->query("INSERT INTO ".__TBL_PAY__."(orderid,kind,uid,title,money,paymoney,addtime,money_list_id,paytime,json) VALUES ('$orderid',$kind,$cook_uid,'$orderid_title',$paymoney,$paymoney,".ADDTIME.",$money_list_id,$paytime,'$json')");
			$payid = $db->insert_id();
			if($kind==7)$jump_url = $jump_url.'&payid='.$payid; 
		}
		switch ($paykind) {
			case 'rmbpay':
				if ($data_money<$paymoney)json_exit(array('flag'=>0,'msg'=>'余额不足'.$paymoney.'元，请选择其它方式支付'));
				switch ($kind) {
					//升级
					case 1:
						$grade  = intval($grade);$if2 = intval($if2);
						$sj_rmb1 = json_decode($_VIP['sj_rmb1'],true);
						$sj_rmb2 = json_decode($_VIP['sj_rmb2'],true);
						$price = ($data_sex==2)?$sj_rmb2[$grade.'_'.$if2]:$sj_rmb1[$grade.'_'.$if2];		
						if($paymoney!=$price)json_exit(array('flag'=>0,'msg'=>'支付金额异常'));
						if($data_grade==$grade){
							$d1  = ADDTIME;
							$d2  = $data_sjtime + $if2*86400;
							$ddiff = $d2-$d1;
							if ($ddiff < 0){//过期
								$sjtime2 = ADDTIME + $if2*86400;
								$SQL     = ",sjtime=".ADDTIME.",sjtime2=".$sjtime2;
							}else{
								$sjtime2 = $data_sjtime2 + $if2*86400;
								$SQL     = ",sjtime2=".$sjtime2;
							}
						}else{
							$sjtime2 = ADDTIME + $if2*86400;
							$SQL     = ",grade=$grade,sjtime=".ADDTIME.",sjtime2=".$sjtime2;
						}
						//牵线
						$qianxian_num = json_decode($_VIP['qianxian_num'],true);
						$crm_qxnum_urole=$qianxian_num[$grade];
						if($crm_qxnum_urole>0)$SQL .= ",crm_qxnum=crm_qxnum+$crm_qxnum_urole";
						//互换
						$hh_num = json_decode($_VIP['hh_num'],true);
						$hh_num_urole=abs(intval($hh_num[$grade]));
						if($hh_num_urole>0)$SQL .= ",hh_num=hh_num+$hh_num_urole";
						//认证
						$rz_num = json_decode($_VIP['rz_num'],true);
						$rz_num_urole=abs(intval($rz_num[$grade]));
						if($rz_num_urole>0)$SQL .= ",rz_num=rz_num+$rz_num_urole";
						//爱豆
						$sj_loveb = json_decode($_VIP['sj_loveb'],true);
						$sj_loveb = intval($sj_loveb[$grade]);
						$sj_loveb_str = '';
						if($sj_loveb>0){
							$SQL .= ",loveb=loveb+$sj_loveb";
							$sj_loveb_str = '（赠送：'.$sj_loveb.$_ZEAI['loveB'].'）';
						}
						//
						$endnum = $data_money-$paymoney;
						//限购
						if(!empty($data_vipnum)){
							$cook_vip_num = json_decode($data_vipnum,true);
							if(@count($cook_vip_num)>0 && is_array($cook_vip_num)){
								$cook_vip_num[$grade] = intval($cook_vip_num[$grade])+1;
							}else{
								$cook_vip_num = array($grade=>1);
							}
						}else{
							$cook_vip_num = array($grade=>1);
						}
						$vipnum = encode_json($cook_vip_num);				
						//
						$db->query("UPDATE ".__TBL_USER__." SET vipnum='$vipnum',money=$endnum".$SQL." WHERE id=".$cook_uid);//grade<=$grade AND 
						setcookie("cook_grade",$grade,time()+31536000,"/",$_ZEAI['CookDomain']);
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',5);	
						if($sj_loveb>0){
							$db->AddLovebRmbList($cook_uid,$orderid_title.$sj_loveb_str,$sj_loveb,'loveb',2);	
						}
						//站内通知
						$C = $data_nickname.'您好，恭喜你'.$orderid_title.'成功'.$sj_loveb_str.'　　<a href='.a('{"kind":"my","mp":"pc"}').' class=aQING>查看详情</a>';
						$db->SendTip($cook_uid,'恭喜你，'.$orderid_title.'成功!',dataIO($C,'in'),'sys');
						//微信模版通知
						if (!empty($data_openid) && $data_subscribe==1){
							$keyword1 = '恭喜你'.$orderid_title.'成功!';
							$keyword3 = urlencode($_ZEAI['siteName']);
							$url      = urlencode(a('{"kind":"my","mp":"mob"}'));
							$remark   = $sj_loveb_str;
							@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$data_openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
						}
						json_exit(array('flag'=>1,'msg'=>utitle($grade).'升级成功','jump_url'=>$jump_url));
					break;
					//loveb充值
					case 2:
						$addloveb_money=$money;
						$loveb_buy     = json_decode($_VIP['loveb_buy'],true);
						$mylovebczzk   = $loveb_buy[$data_grade];
						$loveb_czlist  = json_decode($_VIP['loveb_czlist'],true);;
						foreach ($loveb_czlist as $k=>$bV) {
							$rmb=round($bV/$_ZEAI['loveBrate'],2);
							if($money==$rmb*$mylovebczzk){
								$addloveb_money=$rmb;
								break;
							}
						}
					
						$addloveb = $addloveb_money*$_ZEAI['loveBrate'];
						$db->query("UPDATE ".__TBL_USER__." SET money=money-$paymoney,loveb=loveb+$addloveb WHERE money>=$paymoney AND id=".$cook_uid);
						//爱豆清单入库
						$db->AddLovebRmbList($cook_uid,'余额兑换'.$_ZEAI['loveB'],$addloveb,'loveb',3);		
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,'余额兑换'.$_ZEAI['loveB'],-$paymoney,'money',4);		
						//爱豆到账提醒
						if (!empty($data_openid)){
							$first   = urlencode($data_nickname."您好，您有一笔".$_ZEAI['loveB']."到账！");
							$content = urlencode($orderid_title);
							$url=a('{"kind":"loveb","mp":"mob"}');
							@wx_mb_sent('mbbh=ZEAI_ACCOUNT_IN&openid='.$data_openid.'&num='.$addloveb.'&first='.$first.'&content='.$content.'&url='.$url);
						}
						//人民币-账户资金变动提醒
						if (!empty($data_openid) && $data_subscribe==1){
							$first  = urlencode($data_nickname."您好，您的余额账户有变动：");
							$remark = urlencode("余额兑换".$_ZEAI['loveB']);
							$endnum = $data_money-$paymoney;
							$url    = a('{"kind":"money","mp":"mob"}');
							@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.$url);
						}
						//站内消息2
						$url=a('{"kind":"loveb","mp":"pc"}');
						$C = $data_nickname.'您好，您有一笔'.$_ZEAI['loveB'].'到账！　<a href='.$url.' class=aQING>查看详情</a>';
						$db->SendTip($cook_uid,$_ZEAI['loveB'].'兑换成功',dataIO($C,'in'),'sys');
						json_exit(array('flag'=>1,'msg'=>'充值成功','jump_url'=>$jump_url));
					break;
					//活动报名费
					case 4:
						$endnum = $data_money-$paymoney;
						$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',6);	
						//余额站内消息
						$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
						$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
						
						$tmparrp=explode('fid=',urldecode($jump_url));$fid=intval($tmparrp[1]);
						
						if(ifint($fid))$db->query("UPDATE ".__TBL_PARTY_USER__." SET ifpay=1 WHERE fid=".$fid." AND uid=".$cook_uid);
						//站内通知
						$C = $data_nickname.'您好，恭喜你活动报名费交纳成功!　<a href='.Href('party',$fid).' class=aQING>查看详情</a>';
						$db->SendTip($cook_uid,'恭喜你，'.$orderid_title.'成功!',dataIO($C,'in'),'sys');
						
						//人民币-账户资金变动提醒
						if (!empty($data_openid)){
							$first  = urlencode($nickname."您好，您的余额账户有变动：");
							$remark = urlencode("活动报名费余额支付");
							$endnum = $data_money-$paymoney;
							@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
						}
						json_exit(array('flag'=>1,'msg'=>'交费成功','jump_url'=>$jump_url));				
					break;
					//互选相亲报名费
					case 13:
						//报名入库
						$tmparrp=explode('id=',urldecode($jump_url));$id=intval($tmparrp[1]);
						if(ifint($id)){
							if(!$db->ROW(__TBL_HUXUAN_BMU__,"id","fid=".$id." AND uid=".$cook_uid)){
								$db->query("INSERT INTO ".__TBL_HUXUAN_BMU__."  (fid,addtime,uid) VALUES ($id,".ADDTIME.",$cook_uid)");
								//通知红娘
								$row = $db->ROW(__TBL_HUXUAN__,"title,ifadmlist","id=".$id,"name");
								if ($row){
									$ifadmlist= $row['ifadmlist'];
									$title    = trimhtml(dataIO($row['title'],'out'));
								}
								tell_hn($ifadmlist,'【互选相亲】→【'.$title.'】','报名用户（'.$data_nickname.'，UID：'.$cook_uid.'），余额支付￥'.$pay_money.'元',$jump_url);
								//
								$endnum = $data_money-$paymoney;
								$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
								//余额清单入库
								$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',18);	
								//余额站内消息
								$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
								$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
								//人民币-账户资金变动提醒
								if (!empty($data_openid)){
									$first  = urlencode($nickname."您好，您的余额账户有变动：");
									$remark = urlencode("互选相亲报名费-余额支付");
									$endnum = $data_money-$paymoney;
									@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
								}
								json_exit(array('flag'=>1,'msg'=>'交费报名成功','jump_url'=>$jump_url));
							}
						}
						json_exit(array('flag'=>0,'msg'=>'交费异常，请联系管理员','jump_url'=>$jump_url));
					break;
					case 19:
						//置顶
						$objid=intval($objid);
						$row = $db->ROW(__TBL_TOP__,"day,json_price","id=".$objid,"name");
						if (!$row)json_exit(array('flag'=>0,'msg'=>'亲，OBJID跑路了','jump_url'=>$jump_url));
						$day= $row['day'];
						$json_price = json_decode($row['json_price'],true);
						$price=floatval($json_price[$data_sex.'_'.$data_grade]);
						if($paymoney!=$price)json_exit(array('flag'=>0,'msg'=>'支付金额异常'));
						//
						$row = $db->ROW(__TBL_TOP_U__,"day,addtime2","uid=".$cook_uid);
						if($row){//json_exit(array('flag'=>0,'msg'=>'亲，您已经申请过置顶了，还未过期，无需申请','jump_url'=>$jump_url));
							$day_new = $row['day']+$day;
							$addtime2_new=$row['addtime2']+86400*$day;
							$db->query("UPDATE ".__TBL_TOP_U__." SET day=$day_new,addtime2=$addtime2_new WHERE uid=".$cook_uid);
						}else{
							$addtime2=ADDTIME+86400*$day;
							$db->query("INSERT INTO ".__TBL_TOP_U__."  (fid,uid,day,addtime1,addtime2) VALUES ($objid,$cook_uid,$day,".ADDTIME.",$addtime2)");
						}
						//
						$endnum = $data_money-$paymoney;
						$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',19);	
						//余额站内消息
						$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
						$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
						//人民币-账户资金变动提醒
						if (!empty($data_openid)){
							$first  = urlencode($nickname."您好，您的余额账户有变动：");
							$remark = urlencode("置顶-余额支付");
							$endnum = $data_money-$paymoney;
							@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
						}
						json_exit(array('flag'=>1,'msg'=>'置顶成功，请返回首页查看','jump_url'=>HOST));
					break;
					case 7:
						$endnum = $data_money-$paymoney;
						$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',17);	
						json_exit(array('flag'=>1,'msg'=>'支付成功','jump_url'=>$jump_url.'&paykind='.$paykind));
					break;
					case 8://文章赞赏
/*						$tmparrp=explode('id=',urldecode($jump_url));$id=intval($tmparrp[1]);
						if(ifint($id)){
							$row = $db->ROW(__TBL_NEWS__,"ulist","id=".$id,"name");
							if ($row){
								$ulist = $row['ulist'];
								if(empty($ulist)){
									$newulist[]=$cook_uid;
								}else{
									$newulist=explode(',',$ulist);
									$newulist[]=$cook_uid;
								}
								$newulist=(is_array($newulist))?implode(',',$newulist):'';
								$db->query("UPDATE ".__TBL_NEWS__." SET ulist='$newulist' WHERE id=".$id);
								//
								$endnum = $data_money-$paymoney;
								$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
								//余额清单入库
								$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',12);	
								//余额站内消息
								$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
								$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
								//
								$first  = urlencode($nickname."您好，您的余额账户有变动：");
								$remark = urlencode("文章赞赏-余额支付");
								$endnum = $data_money-$paymoney;
								@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&magapp_uid='.$magapp_uid.'&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
								json_exit(array('flag'=>1,'msg'=>'非常感感谢您的支持！','jump_url'=>$jump_url));
							}
							json_exit(array('flag'=>0,'msg'=>'文章不存在，请联系管理员','jump_url'=>$jump_url));
						}
						json_exit(array('flag'=>0,'msg'=>'交费异常，请联系管理员','jump_url'=>$jump_url));
*/					break;
					case 21:
						//单身群入群费
						$tmparrp=explode('id=',urldecode($jump_url));$id=intval($tmparrp[1]);
						if(ifint($id)){
							if(!$db->ROW(__TBL_QUN_U__,"id","qid=".$id." AND uid=".$cook_uid)){
								$db->query("INSERT INTO ".__TBL_QUN_U__."  (qid,uid,addtime) VALUES ($id,$cook_uid,".ADDTIME.")");
								//通知
								
								//
								if(ifint($id)){
									$SQL = "";
									$row = $db->ROW(__TBL_QUN__,"addloveb,addhhnum,addqxnum","id=".$id,'num');
									if ($row){
										$addloveb= abs(intval($row[0]));
										$addhhnum= abs(intval($row[1]));
										$addqxnum= abs(intval($row[2]));
										if($addloveb>0)$SQL .= ",loveb=loveb+$addloveb";
										if($addhhnum>0)$SQL .= ",hh_num=hh_num+$addhhnum";
										if($addqxnum>0)$SQL .= ",crm_qxnum=crm_qxnum+$addqxnum";
									}
								}
								$endnum = $data_money-$paymoney;
								$db->query("UPDATE ".__TBL_USER__." SET money=$endnum".$SQL." WHERE id=".$cook_uid);
								//爱豆清单入库
								if($addloveb>0)$db->AddLovebRmbList($cook_uid,'加入单身群',$addloveb,'loveb',15);	

								//余额清单入库
								$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',21);	
								//余额站内消息
								$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
								$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
								//人民币-账户资金变动提醒
								//if (!empty($data_openid)){
									$first  = urlencode($nickname."您好，您的余额账户有变动：");
									$remark = urlencode("单身群入群费-余额支付");
									$endnum = $data_money-$paymoney;
									@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&magapp_uid='.$magapp_uid.'&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
								//}
								json_exit(array('flag'=>1,'msg'=>'恭喜您成功加入单身群','jump_url'=>$jump_url));
							}
						}
						json_exit(array('flag'=>0,'msg'=>'交费异常，请联系管理员','jump_url'=>$jump_url));
					break;
					case 22:
						//情感测试费
						$tmparrp=explode('id=',urldecode($jump_url));$id=intval($tmparrp[1]);
						if(ifint($id)){
							$row = $db->ROW(__TBL_LOVETEST_BANK__,"uids","id=".$id,"name");
							if ($row){
								$ulist = $row['uids'];
								if(empty($ulist)){
									$newulist[]=$cook_uid;
								}else{
									$newulist=explode(',',$ulist);
									if(!in_array($cook_uid,$newulist))$newulist[]=$cook_uid;
								}
								$newulist=(is_array($newulist))?implode(',',$newulist):'';
								$db->query("UPDATE ".__TBL_LOVETEST_BANK__." SET uids='$newulist' WHERE id=".$id);
							}
							//通知
							$endnum = $data_money-$paymoney;
							$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);

							//余额清单入库
							$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',22);	
							//余额站内消息
							$C = $data_nickname.'您好，您的余额账户有变动！　<a href='.Href('money').' class=aQING>查看详情</a>';
							$db->SendTip($cook_uid,$orderid_title.'余额支付',dataIO($C,'in'),'sys');
							//人民币-账户资金变动提醒
							$first  = urlencode($nickname."您好，您的余额账户有变动：");
							$remark = urlencode("情感测试费-余额支付");
							$endnum = $data_money-$paymoney;
							@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&magapp_uid='.$magapp_uid.'&openid='.$data_openid.'&num=-'.$paymoney.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url='.urlencode(mHref('money')));
							json_exit(array('flag'=>1,'msg'=>'恭喜您支付成功','jump_url'=>$jump_url));
						}
						json_exit(array('flag'=>0,'msg'=>'交费异常，请联系管理员','jump_url'=>$jump_url));
					break;
					//单次牵线
					case 23:
						$uid=intval($qx_uid);$admid=intval($qx_admid);
						$rowhn = $db->ROW(__TBL_CRM_HN__,"truename,agentid,agenttitle,openid,subscribe,mob","flag=1 AND id=".$admid,'name');
						if($rowhn){
							$admname      =trimhtml(dataIO($rowhn['truename'],'out'));
							$admagentid   =$rowhn['agentid'];
							$admagenttitle=$rowhn['agenttitle'];
							$admopenid   = $rowhn['openid'];
							$admsubscribe= $rowhn['subscribe'];
							$admmob      = $rowhn['mob'];
						}else{
							json_exit(array('flag'=>0,'msg'=>'亲，红娘不存在'));
						}
						$qx_1price = floatval($_VIP['qx_1price']);
						if($paymoney!=$qx_1price)json_exit(array('flag'=>0,'msg'=>'支付金额异常'));
						$endnum = $data_money-$paymoney;
						$db->query("UPDATE ".__TBL_USER__." SET money=$endnum WHERE id=".$cook_uid);
						//余额清单入库
						$db->AddLovebRmbList($cook_uid,$orderid_title.'余额支付',-$paymoney,'money',23);	
						//正式开始
						if(empty($data_admid))$db->query("UPDATE ".__TBL_USER__." SET agentid=$admagentid,agenttitle='$admagenttitle',admid=".$admid.",admname='$admname',admtime=".ADDTIME." WHERE id=".$cook_uid);
						$db->query("INSERT INTO ".__TBL_QIANXIAN__." (admid,admname,uid,senduid,sendkind,addtime) VALUES ($admid,'$admname',$uid,$cook_uid,1,".ADDTIME.")");
						
						//短信
						if(ifmob($admmob) && $sms_onoff['CrmQxHn'] == 1){Zeai_sendsms_authcode($admmob,$cook_uid,'CrmQxHn');}
				
						//微信通知红娘
						if ($admsubscribe == 1 && !empty($admopenid)){
							//客服通知
							$content   = urlencode('【'.$cook_nickname.' uid:'.$cook_uid.'】想委托您为Ta服务，请到CRM工作台->左侧菜单->【线上牵线】进行牵线服务');
							$ret = @wx_kf_sent($admopenid,$content,'text');
							$ret = json_decode($ret);
							//模版通知
							if ($ret->errmsg != 'ok'){
								$keyword1  = $content;
								$keyword3  = urlencode($_ZEAI['siteName']);
								@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$admopenid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url=');
							}
						}
						json_exit(array('flag'=>1,'msg'=>'支付成功，牵线请求已发送成功<br>请等待【'.$admname.'】接洽','jump_url'=>$jump_url.'&paykind='.$paykind));
					break;
				}	
			break;
			case 'wxpay':
				$total_fee = $paymoney*100;//分
				include_once(ZEAI."api/weixin/pay/WxPayPubHelper/WxPayPubHelper.php");
				//微信内部
				if (is_weixin()){
					if(str_len($data_openid) < 10)json_exit(array('flag'=>0,'msg'=>'获取OPENID失败，请点击【我的】重新获取'));
					$jsApi = new JsApi_pub();	
					$unifiedOrder = new UnifiedOrder_pub();	
					$unifiedOrder->setParameter("openid",$data_openid);
					$unifiedOrder->setParameter("out_trade_no",$orderid);//商户订单号 
					$unifiedOrder->setParameter("total_fee",$total_fee);//总金额
					$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
					$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型	
					$unifiedOrder->setParameter("body",$orderid_title);//商品描述
					$unifiedOrder->setParameter("attach",$payid);//附加数据	
					$prepay_id = $unifiedOrder->getPrepayId();
					$jsApi->setPrepayId($prepay_id);
					$jsApiParameters = $jsApi->getParameters();
					$jsApiParameters = json_decode($jsApiParameters,true);
					$db->query("UPDATE ".__TBL_PAY__." SET bz='手机微信支付(内部JSAPI)' WHERE id=".$payid);
					json_exit(array('flag'=>1,'jump_url'=>$jump_url,'trade_type'=>'JSAPI','msg'=>'jsapi调起支付','jsApiParameters'=>$jsApiParameters));
				//MAGAPP
				}elseif(is_mjapp()){
					require_once ZEAI.'cache/config_login.php';
					if(!empty($_LOGIN['mj_domain']) && !empty($_LOGIN['mj_secret']) && !empty($_LOGIN['mj_assistant_secret']) && ifint($magapp_uid)){
						$notify_url = HOST.'/api/magapp/pay/notify_url.php';
						$data_mjpay = array(
							'amount' => $paymoney,
							'trade_no' => $orderid,
							'callback' => $notify_url,
							'title' => $orderid_title,
							'des' => $orderid_title,
							'user_id' => $magapp_uid,
							'remark' => $cook_uid,
							'secret' => $_LOGIN['mj_secret']
						);
						$mjret = Zeai_POST_stream($_LOGIN['mj_domain'].'/core/pay/pay/unifiedOrder',$data_mjpay);
						$mjret = json_decode($mjret,true);
						if($mjret['success']){
							$unionOrderNum = $mjret['data']['unionOrderNum'];//统一订单编号
							$jsApiParameters = array(
								'money' => $paymoney,
								'title' => $orderid_title,
								'des' => $orderid_title,
								'payWay' => array('weixin'=>1,'wallet'=>0),
								'orderNum' => $orderid,
								'unionOrderNum' => $unionOrderNum,
								'type' => '相亲'
							);
							$db->query("UPDATE ".__TBL_PAY__." SET trade_no='$unionOrderNum', bz='手机微信支付(马甲APP)' WHERE id=".intval($payid));
							json_exit(array('flag'=>1,'jump_url'=>$jump_url,'trade_type'=>'MAGAPP','msg'=>'马甲客户端支付','jsApiParameters'=>$jsApiParameters));
						}else{
							json_exit(array('flag'=>0,'msg'=>'统一订单编号获取失败'));
						}
					}else{
						json_exit(array('flag'=>0,'msg'=>'马加APP未配置或magapp_uid跑路了'));
					}
				//H5外部
				}else{
					if(!is_mobile()){exit("请用手机操作");}
					require_once ZEAI.'api/weixin/pay/h5pay_func.php';
					$H5PAY = new www_zeai_cn_h5pay_class();
					$pay_data=array(
						'trade_type'=>"MWEB",
						'appid'=>APPID_,
						'mch_id'=>MCHID_,
						'nonce_str'=>$H5PAY->get_rand_str(32),
						'out_trade_no'=>$orderid,
						'body'=>$orderid_title,
						'total_fee'=>$total_fee,
						'notify_url'=>NOTIFY_URL_,
						'spbill_create_ip'=>$H5PAY->siteip()
					);
					$pay_data['sign'] = $H5PAY->MakeSign($pay_data);
					$pay_vars     = $H5PAY->ToXml($pay_data);
					$re_data      = $H5PAY->curl_post_ssl($pay_vars);
					$wxpay_arr    = $H5PAY->FromXml($re_data);
					if($wxpay_arr['return_code']=="SUCCESS" && $wxpay_arr['result_code']=="SUCCESS"){
						$pay_url  = $wxpay_arr['mweb_url'];
						$pay_url .= '&redirect_url='.urlencode($jump_url);//成功跳转url
						$db->query("UPDATE ".__TBL_PAY__." SET bz='手机微信支付(外部WAP/H5)' WHERE id=".$payid);
						json_exit(array('flag'=>1,'trade_type'=>'H5','msg'=>'H5调起支付','redirect_url'=>$pay_url));
					}else{
						json_exit(array('flag'=>0,'trade_type'=>'H5','msg'=>'商户平台H5支付没开通或参数不正确','redirect_url'=>$jump_url));
					}
				}
			break;
			case 'alipay':
				if(!is_mobile()){exit("请用手机操作");}
				$bzstr = (is_weixin())?'(支付宝)':'(WAP/H5)';
				$db->query("UPDATE ".__TBL_PAY__." SET bz='手机支付宝支付".$bzstr."' WHERE id=".$payid);
				$return_url = urlencode($jump_url);
				$notify_url = urlencode(HOST.'/api/ali/pay/notify_url.php');
				json_exit(array('flag'=>1,'WIDout_trade_no'=>$orderid,'WIDtotal_amount'=>$paymoney,'notify_url'=>$notify_url,'return_url'=>$return_url,'WIDsubject'=>$orderid_title,'WIDbody'=>$orderid_title));
			break;
		}
	break;
	case 'wwwzeaicn-qq:797311':
	
	break;
	case 'ajax_pay'://通用在线支付
		if(!iflogin() && $iflogin==1)json_exit(array('flag'=>'nologin','msg'=>'请您先登录','jumpurl'=>$jumpurl));
		if (is_weixin() && empty($cook_openid) && $iflogin==1){
			json_exit(array('flag'=>'nologin','msg'=>'请您先登录','jumpurl'=>$jumpurl));
		}
		require_once ZEAI.'cache/config_vip.php';
		require_once ZEAI.'cache/config_pay.php';
		$paymoney = abs(round($money,2));
		$orderid_title = trimhtml(dataIO($title,'out'));
		$cook_uid=(!ifint($cook_uid))?$cook_tg_uid:$cook_uid;
		$orderid=(!empty($oid) && $oid!='undefined')?$oid:'WX-'.$cook_uid.'-'.date("YmdHis");
		$tmpid=intval($tmpid);$cook_uid=intval($cook_uid);
		$tg_uid=intval($tg_uid);
		//$db->query("INSERT INTO ".__TBL_PAY__."(orderid,kind,uid,title,money,paymoney,money_list_id,addtime,tg_uid) VALUES ('$orderid',$kind,$cook_uid,'$orderid_title',$money,$money,$tmpid,".ADDTIME.",$tg_uid)");
		//$payid = $db->insert_id();
		$row = $db->ROW(__TBL_PAY__,"id","flag=0 AND orderid='$orderid'",'num');
		if ($row){
			$payid=$row[0];
			$newoid=str_replace(substr($orderid,strrpos($orderid,"-")+1,30),date("YmdHis"),$orderid);
			$db->query("UPDATE ".__TBL_PAY__." SET orderid='$newoid' WHERE id=".$payid);
			$db->query("UPDATE ".__TBL_SHOP_ORDER__." SET orderid='$newoid' WHERE orderid='$orderid'");
			$orderid=$newoid;
		}else{
			$db->query("INSERT INTO ".__TBL_PAY__."(orderid,kind,uid,title,money,paymoney,money_list_id,addtime,tg_uid) VALUES ('$orderid',$kind,$cook_uid,'$orderid_title',$money,$money,$tmpid,".ADDTIME.",$tg_uid)");
			$payid = $db->insert_id();
		}
		$cook_openid=(empty($cook_openid))?$cook_tg_openid:$cook_openid;
		if ($paykind=='wxpay' ){
			$total_fee = $paymoney*100;//分
			include_once(ZEAI."api/weixin/pay/WxPayPubHelper/WxPayPubHelper.php");
			//微信内部
			if (is_weixin()){
				if(str_len($cook_openid) < 10)json_exit(array('flag'=>0,'msg'=>'不好意思，OPENID丢了，请点击【我的】再来重试'));
				$jsApi = new JsApi_pub();	
				$unifiedOrder = new UnifiedOrder_pub();
				$unifiedOrder->setParameter("openid",$cook_openid);
				$unifiedOrder->setParameter("out_trade_no",$orderid);//商户订单号 
				$unifiedOrder->setParameter("total_fee",$total_fee);//总金额
				$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
				$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型	
				$unifiedOrder->setParameter("body",'id:'.$tmpid);//商品描述
				$unifiedOrder->setParameter("attach",$payid);//附加数据	
				$prepay_id = $unifiedOrder->getPrepayId();
				$jsApi->setPrepayId($prepay_id);
				$jsApiParameters = $jsApi->getParameters();
				$jsApiParameters = json_decode($jsApiParameters,true);
				$db->query("UPDATE ".__TBL_PAY__." SET bz='手机微信支付(内部JSAPI)' WHERE id=".$payid);
				json_exit(array('flag'=>1,'jumpurl'=>$jumpurl,'return_url'=>$return_url,'trade_type'=>'JSAPI','msg'=>'jsapi调起支付','jsApiParameters'=>$jsApiParameters));
			//MAGAPP
			}elseif(is_mjapp()){
				require_once ZEAI.'cache/config_login.php';
				if(!empty($_LOGIN['mj_domain']) && !empty($_LOGIN['mj_secret']) && !empty($_LOGIN['mj_assistant_secret']) && ifint($magapp_uid)){
					$notify_url = HOST.'/api/magapp/pay/notify_url.php';
					$data_mjpay = array(
						'amount' => $paymoney,
						'trade_no' => $orderid,
						'callback' => $notify_url,
						'title' => $orderid_title,
						'des' => $orderid_title,
						'user_id' => $magapp_uid,
						'remark' => $cook_uid,
						'secret' => $_LOGIN['mj_secret']
					);
					$mjret = Zeai_POST_stream($_LOGIN['mj_domain'].'/core/pay/pay/unifiedOrder',$data_mjpay);
					$mjret = json_decode($mjret,true);
					if($mjret['success']){
						$unionOrderNum = $mjret['data']['unionOrderNum'];//统一订单编号
						$jsApiParameters = array(
							'money' => $paymoney,
							'title' => $orderid_title,
							'des' => $orderid_title,
							'payWay' => array('weixin'=>1,'wallet'=>0),
							'orderNum' => $orderid,
							'unionOrderNum' => $unionOrderNum,
							'type' => '相亲'
						);
						$db->query("UPDATE ".__TBL_PAY__." SET trade_no='$unionOrderNum', bz='手机微信支付(马甲APP)' WHERE id=".intval($payid));
						json_exit(array('flag'=>1,'jump_url'=>$jump_url,'trade_type'=>'MAGAPP','msg'=>'马甲客户端支付','jsApiParameters'=>$jsApiParameters));
					}else{
						json_exit(array('flag'=>0,'msg'=>'统一订单编号获取失败'));
					}
				}else{
					json_exit(array('flag'=>0,'msg'=>'马加APP未配置或magapp_uid跑路了'));
				}
			//H5外部
			}else{
				if(!is_mobile()){exit("请用手机操作");}
				require_once ZEAI.'api/weixin/pay/h5pay_func.php';
				$H5PAY = new www_zeai_cn_h5pay_class();
				$pay_data=array(
					'trade_type'=>"MWEB",
					'appid'=>APPID_,
					'mch_id'=>MCHID_,
					'nonce_str'=>$H5PAY->get_rand_str(32),
					'out_trade_no'=>$orderid,
					'body'=>$orderid_title,
					'total_fee'=>$total_fee,
					'notify_url'=>NOTIFY_URL_,
					'spbill_create_ip'=>$H5PAY->siteip()
				);
				$pay_data['sign'] = $H5PAY->MakeSign($pay_data);
				$pay_vars     = $H5PAY->ToXml($pay_data);
				$re_data      = $H5PAY->curl_post_ssl($pay_vars);
				$wxpay_arr    = $H5PAY->FromXml($re_data);
				if($wxpay_arr['return_code']=="SUCCESS" && $wxpay_arr['result_code']=="SUCCESS"){
					$pay_url  = $wxpay_arr['mweb_url'];
					$pay_url .= '&redirect_url='.urlencode($return_url);//成功跳转url
					$db->query("UPDATE ".__TBL_PAY__." SET bz='手机微信支付(外部WAP/H5)' WHERE id=".$payid);
					json_exit(array('flag'=>1,'trade_type'=>'H5','msg'=>'H5调起支付','redirect_url'=>$pay_url));
				}else{
					json_exit(array('flag'=>0,'trade_type'=>'H5','msg'=>'商户平台【H5支付没开通】或参数不正确','return_url'=>$return_url));
				}
			}
		}
		exit;
	break;
}
?>